import { MES_XSP_ApiHost } from '@/utils/Interceptors'

// 产品数据类型 - 根据后端实体定义
export interface Product {
  productId: number
  productCode: string
  productName: string
  productSpec?: string
  inventoryUnit: string
  productAttribute: string
  processRouteId?: number
  maxInventory?: number
  minInventory?: number
  safetyInventory?: number
  currentInventory?: number
  productImage?: string
  createTime?: string
  creator?: string
  modifyTime?: string
  modifier?: string
  isDelete: boolean
}

// 产品创建DTO
export interface ProductCreateDTO {
  productCode: string
  productName: string
  productSpec?: string
  inventoryUnit: string
  productAttribute: string
  processRouteId?: number
  maxInventory?: number
  minInventory?: number
  safetyInventory?: number
  currentInventory?: number
  productImage?: string
  creator?: string
}

// 产品更新DTO
export interface ProductUpdateDTO {
  productId: number
  productCode: string
  productName: string
  productSpec?: string
  inventoryUnit: string
  productAttribute: string
  processRouteId?: number
  maxInventory?: number
  minInventory?: number
  safetyInventory?: number
  currentInventory?: number
  productImage?: string
  modifier?: string
}

// 工艺路线选项DTO
export interface ProcessRouteOption {
  processRouteId: number
  routeCode: string
  routeName: string
}

// 产品选项DTO
export interface ProductOption {
  productId: number
  productCode: string
  productName: string
  productSpec?: string
  inventoryUnit: string
}

// API响应类型
export interface ApiResponse<T> {
  code: number
  msg: string
  data: T
}

// 分页响应类型
export interface ApiPaging<T> {
  code: number
  msg: string
  data: any
  totalCount: number
  totalPage: number
  pageData: T[]
}

// 搜索参数类型
export interface ProductSearchParams {
  pageIndex?: number
  pageSize?: number
  productCode?: string
  productName?: string
}

// 获取所有产品列表
export const getAllProducts = async () => {
  return await MES_XSP_ApiHost.get<ApiPaging<Product>>('/api/Product/GetAllProducts')
}

// 获取产品分页列表
export const getProductList = async (params: ProductSearchParams) => {
  return await MES_XSP_ApiHost.get<ApiPaging<Product>>('/api/Product/GetProductList', { params })
}

// 根据ID获取产品详情
export const getProductById = async (productId: number) => {
  return await MES_XSP_ApiHost.get<ApiResponse<Product>>('/api/Product/GetProductById', { 
    params: { productId } 
  })
}

// 创建产品
export const createProduct = async (data: ProductCreateDTO) => {
  return await MES_XSP_ApiHost.post<ApiResponse<boolean>>('/api/Product/CreateProduct', data)
}

// 更新产品
export const updateProduct = async (data: ProductUpdateDTO) => {
  return await MES_XSP_ApiHost.put<ApiResponse<boolean>>('/api/Product/UpdateProduct', data)
}

// 删除产品
export const deleteProduct = async (productId: number) => {
  return await MES_XSP_ApiHost.delete<ApiResponse<boolean>>('/api/Product/DeleteProduct', { 
    params: { productId } 
  })
}

// 批量删除产品
export const batchDeleteProducts = async (productIds: number[]) => {
  return await MES_XSP_ApiHost.delete<ApiResponse<boolean>>('/api/Product/BatchDeleteProduct', { 
    data: productIds 
  })
}

// 根据产品编号获取产品信息
export const getProductByCode = async (productCode: string) => {
  return await MES_XSP_ApiHost.get<ApiResponse<Product>>('/api/Product/GetProductByCode', { 
    params: { productCode } 
  })
}

// 检查产品编号是否存在
export const checkProductCodeExists = async (productCode: string, excludeProductId?: number) => {
  return await MES_XSP_ApiHost.get<ApiResponse<boolean>>('/api/Product/CheckProductCodeExists', { 
    params: { productCode, excludeProductId } 
  })
}

// 获取所有工艺路线（用于下拉框）
export const getAllProcessRoutes = async () => {
  return await MES_XSP_ApiHost.get<ApiResponse<ProcessRouteOption[]>>('/api/Product/GetAllProcessRoutes')
}

// 获取所有产品选项（用于下拉框）
export const getAllProductOptions = async () => {
  return await MES_XSP_ApiHost.get<ApiResponse<ProductOption[]>>('/api/Product/GetAllProductOptions')
}

// 更新产品库存
export const updateProductInventory = async (productId: number, currentInventory: number, modifier?: string) => {
  return await MES_XSP_ApiHost.put<ApiResponse<boolean>>('/api/Product/UpdateProductInventory', null, { 
    params: { productId, currentInventory, modifier } 
  })
}

// 根据产品名称搜索产品
export const searchProductByName = async (productName: string, pageIndex?: number, pageSize?: number) => {
  return await MES_XSP_ApiHost.get<ApiPaging<Product>>('/api/Product/SearchProductByName', { 
    params: { productName, pageIndex, pageSize } 
  })
} 